package com.ms.orm.conditions.update;

import com.ms.common.util.CollectionUtils;
import com.ms.common.util.StringUtils;
import com.ms.orm.conditions.AbstractLambdaWrapper;
import com.ms.orm.conditions.SharedString;
import com.ms.orm.conditions.segments.MergeSegments;
import com.ms.orm.constants.Constants;
import com.ms.orm.util.MetaDataOperator;
import com.ms.orm.util.support.SFunction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Lambda 更新封装
 *
 * @author hubin miemie HCL
 * @since 2018-05-30
 */
@SuppressWarnings("serial")
public class LambdaUpdateWrapper<T> extends AbstractLambdaWrapper<T, LambdaUpdateWrapper<T>>
        implements Update<LambdaUpdateWrapper<T>, SFunction<T, ?>> {

    /**
     * SQL 更新字段内容，例如：name='1', age=2
     */
    private final List<String> sqlSet;

    public LambdaUpdateWrapper() {
        // 如果无参构造函数，请注意实体 NULL 情况 SET 必须有否则 SQL 异常
        this((T) null);
    }

    public LambdaUpdateWrapper(T entity) {
        super.setEntity(entity);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    public LambdaUpdateWrapper(Class<T> entityClass) {
        super.setEntityClass(entityClass);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    LambdaUpdateWrapper(T entity, Class<T> entityClass, List<String> sqlSet, AtomicInteger paramNameSeq,
                        Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias,
                        SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        super.setEntityClass(entityClass);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.paramAlias = paramAlias;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    public LambdaUpdateWrapper<T> set(boolean condition, SFunction<T, ?> column, Object val, String mapping) {
        return maybeDo(condition, () -> {
            //获取值？
            String sql = formatParam(mapping,columnToString(column), val);
            //name= #{入参,jdbcType=NUMERIC}
            sqlSet.add(columnToString(column) + Constants.EQUALS + sql);
        });
    }

    /**
     *
     * @param condition
     * @param sql ，如 name=1
     * @return
     */
    @Override
    public LambdaUpdateWrapper<T> setSql(boolean condition, String sql) {
        if (condition && StringUtils.isNotBlank(sql)) {
            sqlSet.add(sql);
        }
        return typedThis;
    }

    /**
     * name=1,age=2,sex=3
     * @return
     */
    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(Constants.COMMA, sqlSet);
    }

    @Override
    protected LambdaUpdateWrapper<T> instance() {
        return new LambdaUpdateWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
                new MergeSegments(), paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSet.clear();
    }

    /**
     * 获取更新语句,待占位符号
     * @return
     */
    @Override
    public String getFinalSql() {
        StringBuilder builder = new StringBuilder();
        //1、有查询条件，拼接 SELECT
        builder.append(Constants.UPDATE).append(Constants.SPACE)
                .append(MetaDataOperator.getEntityAndTableInfo(getEntityClass()).getTableName()).append(Constants.SPACE)
                .append(Constants.SET).append(Constants.SPACE)
                .append(this.getSqlSet()).append(Constants.SPACE)
                .append(Constants.WHERE).append(Constants.SPACE)
                .append(this.getExpression().getSqlSegment());
        return builder.toString();
    }

}